<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_Photos');
Zend_Loader::loadClass('Zend_Http_Client');

class Picasa {
	
	/**
	 * Returns the full URL of the current page, based upon env variables
	 * 
	 * Env variables used:
	 * $_SERVER['HTTPS'] = (on|off|)
	 * $_SERVER['HTTP_HOST'] = value of the Host: header
	 * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
	 * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
	 *
	 * @return string Current URL
	 */
	public function getCurrentUrl() {
	    global $_SERVER;
	
	    /**
	     * Filter php_self to avoid a security vulnerability.
	     */
	    $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
	    strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
	
	    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
	        $protocol = 'https://';
	    } else {
	        $protocol = 'http://';
	    }
	    $host = $_SERVER['HTTP_HOST'];
	    if ($_SERVER['SERVER_PORT'] != '' &&
	        (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
	        ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
	            $port = ':' . $_SERVER['SERVER_PORT'];
	    } else {
	        $port = '';
	    }
	    return $protocol . $host . $port . $php_request_uri;
	}
	
	/**
	 * Returns the AuthSub URL which the user must visit to authenticate requests 
	 * from this application.
	 *
	 * Uses getCurrentUrl() to get the next URL which the user will be redirected
	 * to after successfully authenticating with the Google service.
	 *
	 * @return string AuthSub URL
	 */
	public function getAuthSubUrl() {
/*
		$client = new Zend_Http_Client('https://www.example.com/', array(
			'adapter'      => 'Zend_Http_Client_Adapter_Proxy',
			'proxy_host'   => 'proxy.shr.secureserver.net',
			'proxy_port'   => 3128
		));
		
		$finalClient = Zend_Gdata_ClientLogin::getHttpClient('email', 'pass', Zend_Gdata_Photos::AUTH_SERVICE_NAME, $client, 'MyCompany-MyApp-1.0');
*/

		$next = $this->getCurrentUrl();
		$scope = 'http://picasaweb.google.com/data';
		$secure = 0;
		$session = 0;
		return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
	}
	
	/**
	 * Returns a HTTP client object with the appropriate headers for communicating
	 * with Google using AuthSub authentication.
	 *
	 * Uses the $_SESSION['sessionToken'] to store the AuthSub session token after
	 * it is obtained.  The single use token supplied in the URL when redirected 
	 * after the user succesfully authenticated to Google is retrieved from the 
	 * $_GET['token'] variable.
	 *
	 * @return Zend_Http_Client
	 */
	public function getAuthSubHttpClient() {
	    global $_SESSION, $_GET;
	    if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
	        $_SESSION['sessionToken'] = 
	            Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
	    } 

	    $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
	    return $client;
	}

	/**
	 * Connection d'un utilisateur.
	 */
	public function getAuthHttpClient($login, $password) {
		$serviceName = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
		$client = Zend_Gdata_ClientLogin::getHttpClient($login, $password, $serviceName);
		return $client;
	}
}
?>